package com.cskaoyan.controller;

import com.cskaoyan.bean.CskaoyanmallGrouponRules;
import com.cskaoyan.bean.bo.ExtensionGrouponRulesBo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.GrouponRulesVo;
import com.cskaoyan.service.ExtensionGrouponRulesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ExtensionGrouponRulesController {

    @Autowired
    ExtensionGrouponRulesService extensionGrouponRulesService;

    // GET /admin/groupon/
    // goodsId=1039051&sort=add_time&order=desc HTTP/1.1

    /**
     * 查询团购信息
     */
    @RequiresPermissions("admin:groupon:list")
    @GetMapping("admin/groupon/list")
    public BaseRespVo getGroupon(Integer page, Integer limit, String sort, String order, Integer goodsId){

        GrouponRulesVo grouponRulesVo = extensionGrouponRulesService.vagueQueryGrouponRules(page, limit, sort, order, goodsId);

        return BaseRespVo.success(grouponRulesVo);
    }

    /**
     * 删除团购规则---逻辑删除
     */
//    POST /admin/groupon/delete HTTP/1.1
    @RequiresPermissions("admin:groupon:delete")
    @PostMapping("admin/groupon/delete")
    public BaseRespVo delGroup(@RequestBody CskaoyanmallGrouponRules grouponRulesBo){

        Integer effectRows = extensionGrouponRulesService.deleteGroupRule(grouponRulesBo);

        if (effectRows != 1) {
            // TODO:
        }

        return BaseRespVo.success();
    }

    /**
     * 编辑(更新团购信息)
     */
//    POST /admin/groupon/update HTTP/1.1
    @RequiresPermissions("admin:groupon:update")
    @PostMapping("admin/groupon/update")
    public BaseRespVo updateGroupRule(@RequestBody ExtensionGrouponRulesBo grouponRulesBo){

//        TODO:要对商品id做一个校验，不能编辑没有的商品

        Integer effectRows = extensionGrouponRulesService.updateGrouponRule(grouponRulesBo);

        if (effectRows != 1) {
            // TODO:
        }

        return BaseRespVo.success();
    }
}
